package com.ziyun.erp.web.modules.quoteParts.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.common.utils.DateUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.erp.modules.quoteParts.entity.QuotePartsEntity;
import com.ziyun.erp.modules.quoteParts.service.QuotePartsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 部件基础表
 *
 * @author  llc
 * @email 
 * @date 2018-09-05 15:06:29
 */
@RestController
@RequestMapping("quoteParts/quoteparts")
public class QuotePartsController {
    @Autowired
    private QuotePartsService quotePartsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("quoteParts:quoteparts:list")
    public R list(@RequestParam Map<String, Object> params){
        //
        PageUtils page = quotePartsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("quoteParts:quoteparts:info")
    public R info(@PathVariable("id") Integer id){
        QuotePartsEntity quoteParts = quotePartsService.selectById(id);

        return R.ok().put("quoteParts", quoteParts);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("quoteParts:quoteparts:save")
    public R save(@RequestBody QuotePartsEntity quoteParts){
        quoteParts.setCreateUser(getCurrentUserName());
        quoteParts.setCreateDate(getCurrentTime());
        quoteParts.setUpdateUser(getCurrentUserName());
        quotePartsService.insert(quoteParts);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("quoteParts:quoteparts:update")
    public R update(@RequestBody QuotePartsEntity quoteParts){
        quoteParts.setUpdateDate(getCurrentTime());
        quoteParts.setUpdateUser(getCurrentUserName());
        ValidatorUtils.validateEntity(quoteParts);
        quotePartsService.updateAllColumnById(quoteParts);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("quoteParts:quoteparts:delete")
    public R delete(@RequestBody Integer[] ids){
        quotePartsService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * @author llc
     * @email
     * @date 2018-09-06 10:14:25
     * 获取当前操作人员
     */
    private static String getCurrentUserName(){
        //获取创建人
        SysUserEntity user=(SysUserEntity)SecurityUtils.getSubject().getPrincipal();
        return user.getUsername();
    }

    /**
     * @author llc
     * @email
     * @date 2018-09-06 10:14:25
     * 获取当前操作时间
     */
    private static Date getCurrentTime(){
        //先将日期转换成字符串
        String stringDate = DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN);
        return DateUtils.stringToDate(stringDate, DateUtils.DATE_TIME_PATTERN);
    }
}
